define('ui/page/list/note/discovery', function (require, exports, module) {
  var $ = require('lib/jquery'),
    Ajax = require('util/ajax'),
    User = require('module/user'),
    Cgi = require('module/cgi'),
    Util = require('module/util'),
    Comment = require('ui/widget/comment'),
    Dialog = require('ui/widget/dialog'),
    template = require('ui/template'),
    undefined;
  var Page = module.exports = {};
  var _isShowingDiscoveryDetail = false;
  
  function updateFollowStatus(ele, id) {
    if (ele.clicked) {
      return;
    }
    ele.clicked = true;
    if (!User.isLogin()) {
      User.login();
      ele.clicked = false;
      return;
    }
    var type = $(ele).attr('d-islike'), url;
    var _this = ele;
    if (type == 1) {
      url = Cgi.get('unfollow');
    } else {
      url = Cgi.get('follow');
    }
    Ajax.get({
      url: url,
      data: {'discovery_id': id},
      success: function (res) {
        if (res.success) {
          switchFollowStatus(res.inlikes, id, ele);
        }
        _this.clicked = false;
      },
      fail: function () {
        _this.clicked = false;
      }
    });
  }
  
  function switchFollowStatus(status, id, dom) {
    var $ele = $('.discovery_like_' + id), $img = $ele.find('img');
    if (status) {
      $ele.attr('d-islike', 1);
    } else {
      $ele.attr('d-islike', 0);
    }
    $img.each(function (index, item) {
      var $item = $(item), src = $item.attr('src');
      if (status) {
        src = src.replace('unfollow', 'followed');
      } else {
        src = src.replace('followed', 'unfollow');
      }
      $item.attr('src', src);
    });
    var num = +$(dom).find('span').text();
    status ? num++ : num--;
    $ele.find('span').text(num);
  }
  
  function discovery_time_factory(time) {
    var time_final = time;
    var time_array = time.split(' ');
    if (time_array.length == 2) {
      var time_date_string = time_array[0];
      var time_time_string = time_array[1];
      var year = time_date_string.split('-')[0];
      var month = time_date_string.split('-')[1];
      var day = time_date_string.split('-')[2];
      var now = new Date();
      if (year == now.getFullYear() && month == (now.getMonth() + 1) && day == (now.getDate())) {
        time_final = time_time_string;
      }
      else {
        time_final = month + '-' + day;
      }
    }
    return time_final;
  }
  
  function fixPosition(context) {
    var top = $(document).scrollTop();
    $(context.dom).css({'top': (top + 20) + 'px'});
  }
  
  function showDiscoveryDetail(data) {
    var discovery = data.discovery, noteTemplate = $('#discovery_template').html();
    var htmlStr = template.parse(noteTemplate, {
      datas: discovery,
      formatTime: discovery_time_factory,
      getImageUrl: Util.getImageUrl
    });
    htmlStr = '<div id="<%=id%>" style="position:absolute;z-index:9999">' + htmlStr + '</div>';
    var comment;
    var discoveryDialog = new Dialog({
      template: htmlStr,
      onCoverClicked: function () {
        this.close();
      },
      onOpened: function () {
        comment = new Comment('.j_comment_container', {'id': discovery.id, 'uid': discovery.user.id});
        fixPosition(this);
        bindDiscoveryEvent();
      },
      onClosed: function () {
        $('.j_discovery_share').unbind('click');
        // $('.j_discover_detail').undelegate('.j_follow', 'click');
        comment && comment.destroy();
        _isShowingDiscoveryDetail = false;
      }
    });
    discoveryDialog.open();
  }
  
  function bindDiscoveryEvent() {
    $('.j_discovery_share').bind('click', function () {
      $CKE && $CKE.over();
    });
    // $('.j_discover_detail').delegate('.j_follow', 'click', function(){
    // 	var id = $(this).parents('.j_discover_detail').attr('d-nid');
    // 	updateFollowStatus(this, id);
    // 	return false;
    // });
    $('.j_discovery_report').bind('click', function () {
      var that = this;
      Dialog.confirm({
        'content': '确定举报？',
        'onSubmitted': function () {
          var id = $(that).parents('.j_discover_detail').attr('d-nid');
          Ajax.get({
            url: Cgi.get('reportToTeam'),
            data: {'discovery_id': id},
            success: function (response) {
              if (response.success) {
                Dialog.notice({'content': '举报成功！'});
              } else {
                Dialog.notice({'content': response.msg});
              }
            }
          });
        }
      });
      return false;
    });
  }
  
  function initEvent() {
    // $('.j_list_container').delegate('.j_follow', 'click', function(){
    // 	var id=$(this).parents('.j_discovery_list').attr('d-nid')
    // 	updateFollowStatus(this, id);
    // 	return false;
    // });
    $('.j_list_container').delegate('.j_note_info', 'click', function (evt) {
      if (_isShowingDiscoveryDetail) {
        return;
      }
      _isShowingDiscoveryDetail = true;
      var id = $(this).parent().attr('d-nid');
      Ajax.get({
        url: Cgi.get('getDisCoveryDetail'),
        data: {'oid': 'discovery.' + id},
        success: function (response) {
          if (response.success) {
            showDiscoveryDetail(response);
          }
        }
      });
      return false;
    });
  }
  
  Page.init = function () {
    initEvent();
  }
});
define('ui/page/list/note/init', function (require, exports, module) {
  var $ = require('lib/jquery'),
    other = require('ui/page/list/note/other'),
    list = require('ui/page/list/note/list'),
    publish = require('ui/page/list/note/publish'),
    discovery = require('ui/page/list/note/discovery'),
    site = require('ui/page/site'),
    undefined;
  var Page = module.exports = {};
  Page.init = function (data) {
    site.init();
    other.init();
    list.init(data);
    publish.init();
    discovery.init();
  }
});
define('ui/page/list/note/list', function (require, exports, module) {
  var $ = require('lib/jquery'),
    Ajax = require('util/ajax'),
    Util = require('module/util'),
    Cgi = require('module/cgi'),
    ImgLazyLoad = require('ui/widget/imglazyload'),
    template = require('ui/template'),
    undefined;
  var Page = module.exports = {};
  var _start_discovery = '';
  var _loadingData = false;
  var _getListTimer;
  var _oid;
  // function initData(){
  // 	_oid = Util.getParam('oid');
  // }
  function updateDiscoveryList() {
    clearTimeout(_getListTimer);
    _getListTimer = setTimeout(function () {
      var $download = $('.pageBottom'),
        top = $(window).height() + $(window).scrollTop();
      if ($download.offset().top <= top) {
        getDiscoveryList();
      }
    }, 200);
  }
  
  function getDiscoveryList() {
    if (_loadingData) {
      return;
    }
    _loadingData = true;
    $('.j_loading_more').show();
    Ajax.get({
      url: Cgi.get('getDiscoveryList'),
      data: {
        'start': _start_discovery,
        'num': 40,
        'oid': _oid
      },
      success: function (res) {
        if (res.success && res.array.length > 0) {
          parseDiscoveryList(res.array);
        } else {
          $(window).unbind('scroll', updateDiscoveryList);
        }
        _loadingData = false;
        $('.j_loading_more').hide();
      }
    });
  }
  
  function parseDiscoveryList(data) {
    _start_discovery = data[data.length - 1].id;
    var htmlArr = [],
      number = $('.j_discovery_list').length,
      cursorIndex = 0,
      templateHtml = $('#list_template').html(),
      classStr = '',
      defaultWidth = 228,
      curData, curHtml;
    for (var i = 0; i < data.length; i++) {
      // if (number + i < 4) {
      // 	classStr = ' top';
      // }else{
      // 	classStr = '';
      // }
      curData = data[i];
      var height;
      height = defaultWidth / curData.width * curData.height;
      curData.height = height;
      var curData = data[i];
      var curHtml = template.parse(templateHtml, {
        datas: curData,
        classStr: classStr,
        getImageUrl: Util.getImageUrl
      });
      if (!htmlArr[cursorIndex]) {
        htmlArr[cursorIndex] = [];
      }
      htmlArr[cursorIndex].push(curHtml);
      if (cursorIndex == 3) {
        cursorIndex = 0;
      } else {
        cursorIndex++;
      }
    }
    for (var j = 0, l = htmlArr.length; j < l; j++) {
      $('.j_list_view_' + j).append(htmlArr[j].join(''));
    }
    ImgLazyLoad.show();
  }
  
  function initEvent() {
    $(window).bind('scroll', updateDiscoveryList);
  }
  
  Page.init = function (data) {
    // initData();
    _oid = data.oid;
    _start_discovery = data.last_discovery_id;
    initEvent();
    // getDiscoveryList(data.last_discovery_id);
    // $(window).trigger('scroll');
  }
});
define('ui/page/list/note/other', function (require, exports, module) {
  var $ = require('lib/jquery'),
    Browser = require('util/browser'),
    Ajax = require('util/ajax'),
    Util = require('module/util'),
    Cgi = require('module/cgi'),
    User = require('module/user'),
    undefined;
  var Page = module.exports = {};
  // var map = {'newest':'全部', 'recommend':'每日精选', '52ce1c02b4c4d649b58b892e':'包包', '52ce1c02b4c4d649b58b892c':'护肤', '52ce1c02b4c4d649b58b892f':'鞋子', '52ce1c02b4c4d649b58b892d':'配饰'
  // 			,'52ce1c02b4c4d649b58b8930':'彩妆', '52ce1c02b4c4d649b58b8933':'服装', '52ce1c02b4c4d649b58b8935':'数码', '52ce1c02b4c4d649b58b8934':'食品'
  // 			,'52ce1c02b4c4d649b58b8936':'母婴', '533e8807b4c4d6619c75148e':'家居', '52ce1c02b4c4d649b58b8931':'男人'};
  // function initDiscoveryTopics(){
  // 	var type = Util.getParam('name');
  // 	Ajax.get({
  // 		url: Cgi.get('getDiscoveryTopics'),
  // 		success: function(res){
  // 			if (res.success) {
  // 				var data = res.data, html = [];
  // 				for(var i=0,l=data.length; i<l; i++){
  // 					var topic = data[i];
  // 					var classStr = 'j_topics_item';
  // 					var oid = topic.oid, arr = oid.split('.'), name = arr[1] || arr[0];
  // 					if (i==0) {
  // 						classStr += ' first';
  // 					};
  // 					if (topic.name == map[type]) {
  // 						classStr += ' active';
  // 					};
  // 					if (topic.name == '每日精选') {
  // 						topic.oid = topic.oid.replace('newest', 'recommend');
  // 						name = 'recommend';
  // 					};
  // 					html.push('<a href="/discovery/topic?oid='+topic.oid+'&name='+name+'" class="'+classStr+'">'+topic.name+'</a>');
  // 				}
  // 				$('.topics_nav').html(html.join(''));
  // 			};
  // 		}
  // 	});
  // }
  function initBackToTop() {
    var width = document.documentElement.clientWidth || document.body.clientWidth, scrollTimer;
    $('.j_back_to_top').css({'right': (width - 980) / 2 - 58 + 'px'});
    var st = $(document).scrollTop();
    if (Browser.ie == '6.0') {
      var top = st + $(window).height() - 66;
      $('.j_back_to_top').css({'top': top});
    }
    if (st > 0) {
      $('.j_back_to_top').fadeIn();
    }
    $(window).bind('scroll', function () {
      clearTimeout(scrollTimer);
      scrollTimer = setTimeout(function () {
        var st = $(document).scrollTop();
        if (st > 0) {
          $('.j_back_to_top').fadeIn();
        } else {
          $('.j_back_to_top').fadeOut();
        }
        if (Browser.ie == '6.0') {
          $('.j_back_to_top').css({'top': st + $(window).height() - 66 + 'px'});
        }
      }, 200);
    });
  }
  
  function initEvent() {
    $('.j_back_to_top').bind('click', function () {
      $(this).hide();
      $('html, body').animate({scrollTop: 0}, 400);
    });
  }
  
  Page.init = function () {
    initBackToTop();
    initEvent();
  }
});
define('ui/page/list/note/publish', function (require, exports, module) {
  var $ = require('lib/jquery'),
    Dialog = require('ui/widget/dialog'),
    Util = require('module/util'),
    template = require('ui/template')
  var Page = module.exports = {};
  
  function showPublishDialog() {
    var publishTemplate = $('#publish_template').html();
    var dialog = new Dialog({
      'template': publishTemplate,
      'destroyWhenClosed': true
    });
    dialog.open();
  }
  
  function initForm() {
    $(".j_publish_note").click(function () {
      showPublishDialog()
    })
  }
  
  Page.init = function () {
    initForm()
  }
});
